package jcer.task;

import java.nio.ByteBuffer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import jcer.core.*;
import jcer.task.util.HttpHelper;

public class HttpTask extends Task {
	static Log logger = LogFactory.getLog(HttpTask.class);

	String url;

	@Override
	public void decode(ByteBuffer buf) throws Exception {
		byte[] msgBuf = new byte[buf.remaining()];
		buf.get(msgBuf);
		url = new String(msgBuf, "utf-8");
	}

	@Override
	public byte[] comsume() {
		logger.debug("comsume task");
		String response = "";
		try {
			HttpHelper httpHelper = new HttpHelper();
			response = httpHelper.get(url);
			logger.info(url);
		} catch (Exception e) {
			response = e.getMessage();
		}

		try {
			byte[] data = response.getBytes("utf-8");
			return data;
		} catch (Exception e) {
			return new byte[0];
		}
	}

}
